package com.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.BiddConfirmBean;
import com.project.bean.SupplierBean;
import com.project.bean.SupplyGoodsBean;
import com.project.dto.ConfirmTenderDto;
import com.project.dto.ConfirmTenderGoodsDto;
import com.project.mapper.IBiddConfirmMapper;
import com.project.mapper.ISupplierMapper;
import com.project.mapper.ISupplyGoodsMapper;
import com.project.service.IBiddConfirmService;
import com.project.util.ConvertBeanUtil;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class BiddConfirmServiceImpl implements IBiddConfirmService {
    @Autowired
    private IBiddConfirmMapper biddConfirmMapper;
    @Autowired
    private ISupplyGoodsMapper supplyGoodsMapper;
    @Autowired
    private ISupplierMapper supplierMapper;
    @Override
    public IPage<BiddConfirmBean> cutByItem(Integer pageNO, Long supplierId, String title, String pact) {
        QueryWrapper<BiddConfirmBean> qw =new QueryWrapper<>();
        qw.eq("fk_supplierId",supplierId);
        qw.orderByDesc("pk_id");
        if (title != null && title.length() != 0) {
            qw.like("bc_title", title);
        }
        if (pact != null && pact.length() != 0) {
            qw.like("bc_pact", pact);
        }
        return biddConfirmMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public BiddConfirmBean findById(Long id) {
        return biddConfirmMapper.selectById(id);
    }

    @Override
    @RabbitListener(queues = "TenderVerifyResult")
    public void add(String json) {
        ConfirmTenderDto confirmTenderDto = JSONObject.parseObject(json, ConfirmTenderDto.class);
//        System.err.println(confirmTenderDto);
        //判断合同号是否存在
        QueryWrapper<BiddConfirmBean> qw = new QueryWrapper<>();
        qw.eq("bc_pact", confirmTenderDto.getPact());
        boolean exists = biddConfirmMapper.exists(qw);
        if (!exists) {

            BiddConfirmBean confirmBean = ConvertBeanUtil.copy(confirmTenderDto, BiddConfirmBean.class);
            SupplierBean supplier = supplierMapper.selectById(confirmBean.getSupplierId());
            confirmBean.setSupplierName(supplier.getSupplierName());
            //添加中标书
            biddConfirmMapper.insert(confirmBean);

            //添加商品
            List<ConfirmTenderGoodsDto> goodsDtoList = confirmTenderDto.getGoodsDtoList();


            List<SupplyGoodsBean> supplyGoodsList = ConvertBeanUtil.copyList(goodsDtoList, SupplyGoodsBean.class);
            System.err.println(supplyGoodsList);
            for (int i = 0; i < supplyGoodsList.size(); i++) {
                SupplyGoodsBean goodsBean = supplyGoodsList.get(i);
                goodsBean.setPact(confirmTenderDto.getPact());
                supplyGoodsMapper.insert(goodsBean);
            }


        }
    }
}
